<template>
    <div class="g-dialog-container" v-loading="loading" element-loading-text="加载中"
         element-loading-spinner="el-icon-loading">
        <div class="g-dialog-content">
            <el-form size="mini" label-suffix="：" :model="ruleForm" status-icon :rules="rules" ref="ruleForm"
                     label-width="100px" class="demo-ruleForm">
                <el-form-item label="岗位名称" prop="name" style="width: 60%">
                    <el-input show-word-limit maxlength="20" placeholder="请输入" size="mini" v-model="ruleForm.name"
                              clearable></el-input>
                </el-form-item>
                <el-form-item label="所属部门" prop="deptId">
                    <el-select v-model="ruleForm.deptId" size="mini" placeholder="请选择" clearable>
                        <el-option
                            v-for="item in options"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="备注" style="width: 80%">
                    <el-input
                        type="textarea"
                        :autosize="{ minRows: 8, maxRows: 8 }"
                        :maxlength="255"
                        resize="none"
                        size="mini"
                        placeholder="请输入"
                        v-model="ruleForm.description"
                        show-word-limit
                    ></el-input>
                </el-form-item>
            </el-form>
        </div>
        <div class="g-dialog-footer">
            <el-button type="primary" size="mini" icon="el-icon-circle-check" @click="handleSave">保存</el-button>
            <el-button type="primary" size="mini" icon="el-icon-circle-close" @click="handleCancel">取消</el-button>
        </div>
    </div>
</template>
<script>
// api
import { addPost, getPost, updatePost } from '@/api/post'
import { getCommonList } from '@/api/common'

export default {
    props: {
        //判断是否进行修改
        isUpdate: {
            type: Boolean,
            default: true
        },
        // 岗位id
        postId: {
            type: [String, Number],
            default: ''
        }
    },
    data() {
        return {
            loading: false,
            ruleForm: {
                id: null,
                name: '',
                deptId: '',
                description: ''
            },
            rules: {
                name: [
                    {
                        required: true,
                        trigger: 'blur',
                        message: '岗位名称不能为空'
                    }
                ],
                deptId: [
                    {
                        required: true,
                        trigger: 'blur',
                        message: '所属部门名称不能为空'
                    }
                ]
            },
            options: []
        }
    },
    created() {
        this.getData()
    },
    methods: {
        getData() {
            getCommonList('dept').then(res1 => {
                if (res1.status !== 200) return this.$msgPrompt.error(res1.msg)
                this.options = res1.obj
                if (this.isUpdate) {
                  this.loading = true
                    getPost(this.postId)
                        .then(res2 => {
                            if (res2.status !== 200) return this.$msgPrompt.error(res2.msg)
                            for (let key in this.ruleForm) {
                                this.ruleForm[key] = res2.obj[key]
                            }
                            this.loading = false

                        })
                } else {
                    this.loading = false
                }
            })

        },
        //保存
        handleSave() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.$startLoading()
                    this.isUpdate ? this.update() : this.add()
                } else {
                    return false
                }
            })
        },
        //添加
        add() {
            addPost(this.ruleForm).then((res) => {
                this.$endLoading()
                if (res.status != 200) return this.$msgPrompt.error(res.msg)
                this.$msgPrompt.success()
                this.$emit('succeed')
            })
        },
        //修改
        update() {
            updatePost(this.ruleForm).then((res) => {
                this.$endLoading()
                if (res.status != 200) return this.$msgPrompt.error(res.msg)
                this.$msgPrompt.success()
                this.$emit('succeed')
            })
        },
        //取消
        handleCancel() {
            this.$emit('close')
        }
    }
}
</script>
<style scoped lang="scss">
.g-dialog-container {
    position: relative;
    width: 100%;
    height: 100%;

    .shade-text {
        display: flex;
        align-items: center;
        margin-bottom: 20px;

        ::v-deep.el-form-item {
            margin-bottom: 0;
        }
    }

    .el-checkbox-group {
        display: flex;
        flex-display: row;
        flex-wrap: wrap;

        .el-checkbox {
            min-width: 100px;
        }
    }


    .el-input,
    .el-select {
        width: 230px;
    }

    .el-textarea {
        width: 562px;
    }

    ul {

        width: 562px;
        height: 190px;
        overflow: auto;

    }

    .menu-tree {
        display: inline-block;
        vertical-align: top;
        width: 270px;

        ::v-deep .el-tree {
            .el-tree-node {
                &:focus > .el-tree-node__content {
                    background-color: transparent;
                }
            }

            .el-tree-node__content {
                &:hover {
                    background-color: transparent;
                }
            }
        }
    }
}
</style>
